<?php
namespace App\Controller;
use Cake\Collection\Collection;
use Cake\Cache\Cache;

class CategoriesController extends AppController{

    public $_cache_config = 'categories';
    public function initialize()
    {
        parent::initialize();
        $this->loadComponent('CategoryCache');
    }

    /*
   * create by phpstorm
   * author：Ming
   * date：2020.11.17
   * ps：批量新增分类
   * */
    public function importCategories()
    {
        if(!$this->request->is("post")){
            $this->G->error("post_error");
            return null;
        }
        $categories = $this->request->getData("categories");
        if(empty($categories) || !is_array($categories)){
            $this->G->error("categories_error");
            return null;
        }
        foreach ($categories as $k=>$v){
            $category = $this->Categories->newEntity();
            $category->alias = $v['parent'];
            $category->parent_id = 0;
            $category->enabled = 1;
            $category_save = $this->Categories->save($category);
            if(!empty($v['childNode'])){
                foreach ($v['childNode'] as $key=>$value){
                    $category = $this->Categories->newEntity();
                    $category->alias = $value;
                    $category->parent_id = $category_save->id;
                    $category->enabled = 1;
                    $this->Categories->save($category);
                }
            }
        }
        $this->G->success('Success');
    }

    /*
   * create by phpstorm
   * author：Ming
   * date：2020.04.01
   * ps：新增分类
   * */
    public function add()
    {
        if(!$this->request->is("post")){
            $this->G->error("post_error");
            return null;
        }
        $parent_id = $this->request->getData("parent_id");
        $alias = $this->request->getData("alias");
        $enabled = $this->request->getData("enabled");
        $home_show = $this->request->getData("home_show");
        $icon = $this->request->getData("icon");
        $icon_active = $this->request->getData("icon_active");
        $gather = $this->request->getData("gather");
        $parent_id = empty($parent_id) ? null : $parent_id;
        $result = $this->CategoryCache->add($alias, $parent_id, $enabled, $home_show, $icon, $icon_active, $gather);
        if(is_string($result)){
            $this->G->error($result);
            return null;
        }
        if(!empty($parent_id)){
            Cache::delete('category_'.$parent_id, $this->_cache_config);
        }
        $this->G->success('Success');
    }

    /*
   * create by phpstorm
   * author：Ming
   * date：2020.04.02
   * ps：修改分类
   * */
    public function update()
    {
        if(!$this->request->is("post")){
            $this->G->error("post_error");
            return null;
        }
        $id = $this->request->getData("id");
        $parent_id = $this->request->getData("parent_id");
        $alias = $this->request->getData("alias");
        $enabled = $this->request->getData("enabled");
        if(!is_numeric($id)){
            $this->G->error("id_error");
            return null;
        }
        if(!is_numeric($parent_id) && empty($alias) && !is_numeric($enabled)){
            $this->G->error("data_error");
            return null;
        }
        if(is_numeric($parent_id)){
            $data['parent_id'] = $parent_id;
        }
        if(!empty($alias)){
            $data['alias'] = $alias;
        }
        if(is_numeric($enabled)){
            $data['enabled'] = $enabled;
        }
        if(!empty($data['icon'])){
            $data['icon'] = $this->G->setImgUrl($data['icon']);
        }
        if(!empty($data['icon_active'])){
            $data['icon_active'] = $this->G->setImgUrl($data['icon_active']);
        }
        $result = $this->CategoryCache->updateCache($id, $data);
        if(is_string($result)){
            $this->G->error($result);
            return null;
        }
        if(!empty($parent_id)){
            Cache::delete('category_'.$parent_id, $this->_cache_config);
        }
        $this->G->success('Success');
    }

    /*
   * create by phpstorm
   * author：Ming
   * date：2020.04.02
   * ps：删除分类
   * */
    public function delete()
    {
        if(!$this->request->is("post")){
            $this->G->error("post_error");
            return null;
        }
        $id = $this->request->getData("id");
        if(!is_numeric($id)){
            $this->G->error("id_error");
            return null;
        }
        $result = $this->CategoryCache->delete($id);
        if(is_string($result)){
            $this->G->error($result);
            return null;
        }
        $this->G->success('Success');
    }

    /*
   * create by phpstorm
   * author：Ming
   * date：2020.04.02
   * ps：获取分类列表
   * */
    public function getList()
    {
        $title = $this->request->getData("title");
        $where = [];
        if(!empty($title)){
            $where['alias like'] = '%'.$title.'%';
        }
        $categories = $this->Categories->find()
            ->select(['id','label'=>'alias','parent_id','enabled','home_show','icon','icon_active'])
            ->where([$where])
            ->order(['lft asc'])
            ->toArray();
        if(!empty($categories)){
            $collection = new Collection($categories);
            $categories = $collection->nest('id', 'parent_id')->toList();
        }
        $this->G->success('Success', $categories);
    }

    /*
   * create by phpstorm
   * author：Ming
   * date：2020.04.02
   * ps：获取指定分类
   * */
    public function getCategory()
    {
        if(!$this->request->is("post")){
            $this->G->error("post_error");
            return null;
        }
        $id = $this->request->getData("id");
        if(!is_numeric($id)){
            $this->G->error("id_error");
            return null;
        }
        $result = $this->CategoryCache->getVal($id);
        $this->G->success('Success', $result);
    }

    /*
   * create by phpstorm
   * author：Ming
   * date：2020.04.02
   * ps：分类移动
   * */
    public function move()
    {
        if(!$this->request->is("post")){
            $this->G->error("post_error");
            return null;
        }
        $id = $this->request->getData("id");
        $action = $this->request->getData("action");
        if(!is_numeric($id)){
            $this->G->error("id_error");
            return null;
        }
        if(!in_array($action, ['up','down'])){
            $this->G->error("action_error");
            return null;
        }
        $result = $this->CategoryCache->move($id, $action);
        if(is_string($result)){
            $this->G->error($result);
            return null;
        }
        $this->G->success('Success', $result);
    }

}
?>